package birdry.utils	{
	
	//	import birdry.utils.TextFieldUtil;
	
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import birdry.utils.StringUtil;
	import flash.geom.ColorTransform;
	
	public class TextFieldUtil	{
		
		public static const SMOOTH_COUNT:String = "smooth_count";
		public static const APPEAR_RANDOM_TEXT:String = "appear_tandom_text";
		
		public static function setTextFormat( field:TextField, property:Object, beginIndex:int = -1, endIndex:int = -1 ): void	{
			var format:TextFormat = new TextFormat();
			for ( var i:String in property )	{
				format[i] = property[i];
			}
			field.setTextFormat( format, beginIndex, endIndex );
		}
		
		
		public static function setFieldSelectionColor( field:TextField, color:uint ):void		{
			field.backgroundColor = invert( field.backgroundColor );
			field.borderColor = invert( field.borderColor );
			field.textColor = invert( field.textColor );
				
			var colorTrans:ColorTransform = new ColorTransform();
			colorTrans.color = color;
			colorTrans.redMultiplier = -1;
			colorTrans.greenMultiplier = -1;
			colorTrans.blueMultiplier = -1;
			field.transform.colorTransform = colorTrans;
			
			function invert( color:uint ):uint			{
				var colorTrans:ColorTransform = new ColorTransform();
				colorTrans.color = color;
				return invertColorTransform( colorTrans ).color;
			}
			
			function invertColorTransform( colorTrans:ColorTransform ):ColorTransform			{
				with( colorTrans )				{
					redMultiplier = -redMultiplier;
					greenMultiplier = -greenMultiplier;
					blueMultiplier = -blueMultiplier;
					redOffset = 255 - redOffset;
					greenOffset = 255 - greenOffset;
					blueOffset = 255 - blueOffset;
				}
				return colorTrans;
			}
		}
		
		
		public static function smoothCount(txt:TextField, origin:Number, target:Number, time:Number=0.05): void	{
			txt.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			function onEnterFrame( e: Event ): void	{
				if (Math.abs(target - origin) < 1)	{
					origin = target;
					txt.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
				}
				origin += time * (target - origin);
				var value:String = String(Math.floor(origin));
				txt.text = value;
				txt.dispatchEvent( new Event( TextFieldUtil.SMOOTH_COUNT ));
			}
		}
		
		/**
		* 텍스트 필드의 자간을 줄여 보아요
		* TextFieldUtil.letterSpacing(txt, -0.5);
		* @param	contentTextField	:	자간을 줄일 텍스트필드
		* @param	value				:	줄일 자간 값
		* @return
		*/
		public static function letterSpacing(contentTextField:TextField, value:Number): void	{
			var txtFormat:TextFormat = new TextFormat();
			txtFormat.letterSpacing = value;
			contentTextField.setTextFormat(txtFormat);
		}
		
		public static function inputBugRepair(txt:TextField): void	{
			txt.alwaysShowSelection = true;
			txt.addEventListener( FocusEvent.FOCUS_OUT, onFocusOut);
			function onFocusOut( e: FocusEvent ): void	{
				txt.setSelection(0, 0);
			}
		}
		
		/**
		 * 인풋텍스트에 기본 문자값을 셋팅한다.
		 * TextFieldUtil.inputTextSet(txt);
		 * @param	txt	:	적용시킬 텍스트인풋
		 */
		public static function textInputSet(txt:TextField): void	{
			var originStr:String = txt.text;
			txt.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
			txt.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
			function onFocusIn( e: FocusEvent ): void	{
				if (txt.text == originStr)		txt.text = "";
			}
			function onFocusOut( e: FocusEvent ): void	{
				if (txt.text == "")		txt.text = originStr;
			}
			txt.addEventListener(Event.ADDED_TO_STAGE, onAddedStage);
			txt.addEventListener(Event.REMOVED_FROM_STAGE, onRemoveStage);
			function onRemoveStage( e: Event ): void	{
				txt.removeEventListener(FocusEvent.FOCUS_IN, onFocusIn);
				txt.removeEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
			}
			function onAddedStage( e: Event ): void	{
				txt.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
				txt.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
			}
		}
		
		/**
		* StringUtil.appearRandomText(textMc, "동해물과백두산이");	-->		"동해물....";
		* @param	Text	:	텍스트 필드
		* @param	Char	:	표현할 문자열
		* @return
		*/
		public static function appearRandomText(_textField:*, _str:String): void 	{
			var textLength:uint = _str.length;
			var i:uint = 0;
			var txt:String = "";
			var ranText:String;
			_textField.addEventListener(Event.ENTER_FRAME, enterFrame);
			function enterFrame(e:Event):void	{
				if (i < textLength+1) 		{
					i++;
					ranText = "";
					var j:uint = 0;
					if(j < textLength - txt.length)  ranText = ranText+String.fromCharCode(MathUtil.getRandomInt(0,57)+65);
					while (j < textLength - txt.length) {
						// ranText = ranText+String.fromCharCode(MathUtil.getRandomInt(0,57)+65);
						ranText += "-";
						j++;
					}
					txt = _str.substring(0, i);
					_textField.text = txt + ranText;
					_textField.dispatchEvent( new Event( TextFieldUtil.APPEAR_RANDOM_TEXT ));
				}
				else	{
					_textField.removeEventListener(Event.ENTER_FRAME, enterFrame);
				}
			}
		}
		
		/**
		* 텍스트 필드 길이만큼 문자를 자른다.
		* @param	txt				:	문자를 자를 텍스트필드
		* @param	replaceText		:	자른후 집어넣을 문자
		* @param	replaceWidth	:	replaceText 의 확보영역
		* @return
		*/
		public static function restrictTF(txt : TextField, replaceText : String = "...", replaceWidth : int = 20) : void	{
			var maxX : Number = 0;
			var maxY : Number = 4;
			var replace : Boolean = false;
			var txtWidth:uint = Math.floor(txt.width);
			if (!txt.multiline) { 
				if (txt.textWidth > txtWidth) {
					maxX = txtWidth - replaceWidth;
					maxY = txt.getLineMetrics(0).height >> 1;
					replace = true;
				}
			} else { 
				var i : int;
				var metricHeight : Number;
				for (i = 0; i < txt.numLines; i++) {
					metricHeight = txt.getLineMetrics(i).height;
					if (maxY + metricHeight < txt.height) {
						maxY += metricHeight;
					} else {
						maxX = txtWidth - replaceWidth;
						maxY -= 10;
						replace = true;
						break;
					}
				}
			}
			if (replace) {
				var end : int = txt.getCharIndexAtPoint(maxX, maxY);
				if (end > 0) {
					txt.replaceText(end, txt.length, replaceText);
				}
			}
		}
		
		
		public static function scrollTF(txt: TextField, divideStr:String = "", selectDirect:String = "LEFT"): void	{
			if (txt.textWidth < txt.width)	return;
			var str:String = txt.text;
			txt.text = str + divideStr + str + divideStr + str + divideStr;
			
			txt.addEventListener(Event.ADDED_TO_STAGE, onAdded);
			txt.addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
			txt.dispatchEvent( new Event( Event.ADDED_TO_STAGE ));
			function onAdded( e: Event ): void	{
				txt.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
			function onRemoved( e: Event ): void	{
				txt.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
			
			txt.dispatchEvent( new Event( "scroll_end" ));
			txt.addEventListener("scroll_end", function():void	{	txt.removeEventListener(Event.ENTER_FRAME, onEnterFrame)	} );
			txt.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
			if (selectDirect == "RIGHT")	txt.scrollH = txt.scrollH - txt.textWidth / 3;
			function onEnterFrame( e: Event ): void	{
				if (selectDirect == "LEFT")	{
					txt.scrollH ++;
					if(txt.scrollH > txt.maxScrollH - 10)	{
						txt.scrollH = txt.scrollH - txt.textWidth / 3;
					}
				}
				if (selectDirect == "RIGHT")	{
					txt.scrollH --;
					if(txt.scrollH < 1)	{
						txt.scrollH = txt.scrollH - txt.textWidth / 3;
					}
				}
			}
		}
		
		public static function scrollBothTF(txt:TextField): void	{
			var directLeft:Boolean = true;
			txt.addEventListener(Event.ADDED_TO_STAGE, onAdded);
			txt.addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
			txt.dispatchEvent( new Event( Event.ADDED_TO_STAGE ));
			function onAdded( e: Event ): void	{
				txt.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
			function onRemoved( e: Event ): void	{
				txt.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
			function onEnterFrame( e: Event ): void	{
				if (directLeft)	{
					txt.scrollH ++;
					if(txt.scrollH > txt.maxScrollH-1)	{
						directLeft = false;
					}
				}	else 	{
					txt.scrollH --;
					if(txt.scrollH < 1)	{
						directLeft = true;
					}
				}
			}
		}
	}	// class
}	// package